package com.base.mail.presenter;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;

import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.Partner;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.RxBus2;
import com.base.mail.adapter.AJParterAdapter;
import com.base.mail.bean.PartListBean;
import com.base.mail.tree.Node;
import com.base.mail.tree.OnTreeNodeClickListener;
import com.base.mail.view.AjPartenerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


/**
 * Created by weikailiang on 2019/11/13.
 */

public class AjPartenerPrestener extends BasePresenter<AjPartenerView> {
    public AJParterAdapter mAdapter;
    private List<Partner> mCurrentPartners = new ArrayList<>();
    public AjPartenerPrestener(AjPartenerView view, Activity activity) {
        super(view, activity);
    }

    @Override
    public void start() {
        super.start();
        mCurrentPartners.clear();
        mCurrentPartners.addAll(BaseApp.getPartners());
        String str = "";
        if (mCurrentPartners.size() != 0) {
            for (Partner partner : mCurrentPartners) {
                str += partner.getUserName() + ",";
            }
            mView.returnPartner(str.substring(0,str.length()-1));
        }


        Disposable disposable = RxBus2.getInstance().doSubscribe(Node.class, new Consumer<Node>() {
            @Override
            public void accept(Node bean) throws Exception {
                String str = "";
                for (Map.Entry<String,Node> part: AJParterAdapter.mMapPart.entrySet()) {
                    str += part.getValue().getName() + ",";
                }
                if (str!="") {
                    mView.returnPartner(str.substring(0, str.length() - 1));
                }
            }
        });
        RxBus2.getInstance().addSubscription(this, disposable);
    }

    public void getParenter(){
        mView.showPb();
       HashMap<String, String> param = new HashMap<>();
       Disposable enqueue = RealNetWorkUtil.postContent().method("api-basic/dept/phoneDeptList").content(param)
               .build().enqueueForArray(PartListBean.class, new CallBackImpl<List<PartListBean>>() {
                   @Override
                   public void onError(int code, String msg, String errMsg) {
                       mView.hidePb();
                   }

                   @Override
                   public void onNext(List<PartListBean> users) {
                       mView.hidePb();
                       parse(users);
                   }
               });

       mCompositeDisposable.add(enqueue);
   }

    private void parse(List<PartListBean> list){
        final List<Node> mData = new ArrayList<>();
        Node node = null;
        for (PartListBean bean : list){

            node = new Node();

            node.setId(bean.getId());
            node.setpId(bean.getParentId());
            node.setName(TextUtils.isEmpty(bean.getText())?bean.getName():bean.getText());
            node.setState(bean.getType()+"");
            if (!TextUtils.isEmpty(bean.getPhone())) {
                node.setPhone(bean.getPhone());
            }
            for (Partner partner : mCurrentPartners){
                if (partner.getId().equals(bean.getId())){
                    node.setChecked(true);
                    AJParterAdapter.mMapPart.put(node.getId().toString(),node);
                }
            }
            mData.add(node);

        }
        mView.getRecycler().setLayoutManager(new LinearLayoutManager(mActivity));
        //第一个参数  RecyclerView
        //第二个参数  上下文
        //第三个参数  数据集
        //第四个参数  默认展开层级数 0为不展开
        //第五个参数  展开的图标
        //第六个参数  闭合的图标
        mAdapter = new AJParterAdapter(mView.getRecycler(),mActivity,
                mData, 2);
        mView.getRecycler().setAdapter(mAdapter);
        mAdapter.setOnTreeNodeClickListener(new OnTreeNodeClickListener() {
            @Override
            public void onClick(Node node, int position) {
                if (node.getState().equals("6")){

                }
            }
        });

    }
}
